<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="审核对象ID：承包商ID, 项目ID, 作业计划ID" prop="targetId">
        <el-input
          v-model="queryParams.targetId"
          placeholder="请输入审核对象ID：承包商ID, 项目ID, 作业计划ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="审核配置id" prop="configId">
        <el-input
          v-model="queryParams.configId"
          placeholder="请输入审核配置id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="审核步骤" prop="approvalStep">
        <el-input
          v-model="queryParams.approvalStep"
          placeholder="请输入审核步骤"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="审核人ID" prop="approvalUserId">
        <el-input
          v-model="queryParams.approvalUserId"
          placeholder="请输入审核人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="审核人" prop="approvalUserName">
        <el-input
          v-model="queryParams.approvalUserName"
          placeholder="请输入审核人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核类型" prop="approvalType">
        <el-select
          v-model="queryParams.approvalType"
          placeholder="请选择审核类型"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="option in approvalTypeOptions"
            :key="option.value"
            :label="option.label"
            :value="option.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="approvalStatus">
        <el-select
          v-model="queryParams.approvalStatus"
          placeholder="请选择审核状态"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="option in approvalStatusOptions"
            :key="option.value"
            :label="option.label"
            :value="option.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="审核时间" prop="approvalTime">
        <el-date-picker clearable
          v-model="queryParams.approvalTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择审核时间">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="是否需要签字: 0否 1是" prop="requireSign">
        <el-input
          v-model="queryParams.requireSign"
          placeholder="请输入是否需要签字: 0否 1是"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="签字URL" prop="signUrl">
        <el-input
          v-model="queryParams.signUrl"
          placeholder="请输入签字URL"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['approval:result:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['approval:result:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['approval:result:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['approval:result:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="resultList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="审核结果主键" align="center" prop="id" />
      <el-table-column label="审核对象ID：承包商ID, 项目ID, 作业计划ID" align="center" prop="targetId" />
      <el-table-column label="审核对象类型：准入，开工申请，作业申请" align="center" prop="targetType" />
      <el-table-column label="审核配置id" align="center" prop="configId" />
      <el-table-column label="审核类型" align="center" prop="approvalType">
        <template slot-scope="scope">
          <span v-if="scope.row.approvalType === 1">准入审核</span>
          <span v-else-if="scope.row.approvalType === 2">开工审核</span>
          <span v-else-if="scope.row.approvalType === 3">作业审核</span>
          <span v-else>{{ scope.row.approvalType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核步骤" align="center" prop="approvalStep" />
      <el-table-column label="审核人ID" align="center" prop="approvalUserId" />
      <el-table-column label="审核人姓名" align="center" prop="approvalUserName" />
      <el-table-column label="审核状态" align="center" prop="approvalStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.approvalStatus === 1">待审</span>
          <span v-else-if="scope.row.approvalStatus === 2">通过</span>
          <span v-else-if="scope.row.approvalStatus === 3">退回修改</span>
          <span v-else>{{ scope.row.approvalStatus }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核意见" align="center" prop="comment" />
      <el-table-column label="审核时间" align="center" prop="approvalTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.approvalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否需要签字: 0否 1是" align="center" prop="requireSign" />
      <el-table-column label="签字URL" align="center" prop="signUrl" />
      <el-table-column label="审核步骤是否必须: 0可以跳过-认定状态通过 1必须" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['approval:result:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['approval:result:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改审核结果对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核对象ID：承包商ID, 项目ID, 作业计划ID" prop="targetId">
          <el-input v-model="form.targetId" placeholder="请输入审核对象ID：承包商ID, 项目ID, 作业计划ID" />
        </el-form-item>
        <el-form-item label="审核配置id" prop="configId">
          <el-input v-model="form.configId" placeholder="请输入审核配置id" />
        </el-form-item>
        <el-form-item label="审核步骤" prop="approvalStep">
          <el-input v-model="form.approvalStep" placeholder="请输入审核步骤" />
        </el-form-item>
        <el-form-item label="审核人ID" prop="approvalUserId">
          <el-input v-model="form.approvalUserId" placeholder="请输入审核人ID" />
        </el-form-item>
        <el-form-item label="审核人姓名" prop="approvalUserName">
          <el-input v-model="form.approvalUserName" placeholder="请输入审核人姓名" />
        </el-form-item>
        <el-form-item label="审核意见" prop="comment">
          <el-input v-model="form.comment" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="审核时间" prop="approvalTime">
          <el-date-picker clearable
            v-model="form.approvalTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否需要签字: 0否 1是" prop="requireSign">
          <el-input v-model="form.requireSign" placeholder="请输入是否需要签字: 0否 1是" />
        </el-form-item>
        <el-form-item label="签字URL" prop="signUrl">
          <el-input v-model="form.signUrl" placeholder="请输入签字URL" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listResult, getResult, delResult, addResult, updateResult } from "@/api/cbs/approval/result"

export default {
  name: "ApprovalResult",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 审核结果表格数据
      resultList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        targetId: null,
        targetType: null,
        configId: null,
        approvalType: null,
        approvalStep: null,
        approvalUserId: null,
        approvalUserName: null,
        approvalStatus: null,
        comment: null,
        approvalTime: null,
        requireSign: null,
        signUrl: null,
        status: null,
      },
      // 审核类型选项数组
      approvalTypeOptions: [
        { label: "全部", value: "" },
        { label: "准入审核", value: 1 },
        { label: "开工审核", value: 2 },
        { label: "作业审核", value: 3 }
      ],
      // 审核状态选项数组
      approvalStatusOptions: [
        { label: "全部", value: "" },
        { label: "待审", value: 1 },
        { label: "通过", value: 2 },
        { label: "退回修改", value: 3 }
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询审核结果列表 */
    getList() {
      this.loading = true
      listResult(this.queryParams).then(response => {
        this.resultList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        targetId: null,
        targetType: null,
        configId: null,
        approvalType: null,
        approvalStep: null,
        approvalUserId: null,
        approvalUserName: null,
        approvalStatus: null,
        comment: null,
        approvalTime: null,
        requireSign: null,
        signUrl: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加审核结果"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getResult(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改审核结果"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateResult(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addResult(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除审核结果编号为"' + ids + '"的数据项？').then(function() {
        return delResult(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('approval/result/export', {
        ...this.queryParams
      }, `result_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
